package com.pingbu.tijian.hospital.backapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.Student;
import com.pingbu.tijian.hospital.backapi.bean.entity.Teacher;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserInfo;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserPermission;
import com.pingbu.tijian.hospital.backapi.config.GlobalConst;
import com.pingbu.tijian.hospital.backapi.mapper.TeacherMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserInfoMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserPermissionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.awt.*;
import java.util.List;

@Service
public class TeacherService {

    private static Logger logger = LoggerFactory.getLogger(StudentZuoyeStatistService.class);

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserPermissionMapper userPermissionMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    public ResultBean<PageInfo<Teacher>> list( Teacher teacher) {
        PageHelper.startPage(teacher.getPageNum(), teacher.getPageSize());
        List<Teacher> lst1 = teacherMapper.list(teacher);
        System.out.println(lst1);
        PageInfo<Teacher> pi1 = new PageInfo<>(lst1);
        //将查询到的数据传入PageInfo
        return ResultBean.create(0, "success", pi1);
    }

    public ResultBean<Integer> edit(Teacher teacher) {
        try {
            teacherMapper.edit(teacher);
        } catch (Exception err) {
            return ResultBean.create(23, err.getMessage());
        }
        return ResultBean.create(0, "success");
    }

    public ResultBean<Integer> delete(Teacher teacher) {
        teacherMapper.delete(teacher);
        return ResultBean.create(0,"success");
    }



    public ResultBean<Integer> insert(Teacher teacher) {
        teacher.setNoid(PbSecretUtils.uuid());
        //获取老师对应对应的用户
        UserInfo userInfo = new UserInfo();
        userInfo.setUsercode(teacher.getBind_usercode());
        UserInfo teacherUserInfo = userInfoMapper.get(userInfo);
        //添加老师的权限
        UserPermission up = new UserPermission();
        up.setNoid(PbSecretUtils.uuid());
        up.setUser_id(teacherUserInfo.getNoid());
        up.setPermission_value(GlobalConst.ClassManage);
        userPermissionMapper.insert(up);

        teacherMapper.insert(teacher);
        return ResultBean.create(0,"success");
    }
}
